package com.shenjinzheng.utils;

import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.security.Keys;
import org.springframework.stereotype.Component;

import java.nio.charset.StandardCharsets;
import java.security.Key;
import java.util.Date;

/**
 * @author wuyunbin
 */
@Component
public class JwtUtil {

    public String createToken(Claims claims, Key key){
        return Jwts.builder()
                .setClaims(claims)
                .setIssuedAt(new Date())
                .setExpiration(new Date(System.currentTimeMillis() + 3600000))
                .signWith(key)
                .compact();
    }

    public static void main(String[] args) {
        JwtUtil jwtUtil=new JwtUtil();

        Claims claims=Jwts.claims();
        claims.put("id","1");
        Key key= Keys.hmacShaKeyFor("12345689012345678901234567890123456890123456789012345678901234568901234567890123456789012345689012345678901234567890".getBytes(StandardCharsets.UTF_8));

        String token = jwtUtil.createToken(claims, key);
        System.out.println(token);
    }
}